package jmu.edu.yjc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.bo.MessageBo;
import jmu.edu.yjc.domain.entity.Message;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.domain.vo.message.UnreadVo;
import jmu.edu.yjc.domain.vo.user.UserContactVo;
import jmu.edu.yjc.mapper.MessageMapper;
import jmu.edu.yjc.service.MessageService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 消息记录表(Message)表服务实现类
 *
 * @author makejava
 * @since 2023-01-20 19:09:47
 */
@Service("messageService")
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    public Message getLassMessageById(Long fromUserId,Long toUserId) {
        return baseMapper.getLassMessageById(fromUserId,toUserId);
    }

    @Override
    public PageVo getMessages(Integer pageNumber, Integer pageSize,Long userId, Long toUserId) {
        Integer res = (pageNumber-1)*pageSize;
        Integer tar = (pageNumber)*pageSize;
        List<MessageBo> messages = baseMapper.getMessages(res, tar, userId, toUserId);
        Integer total = baseMapper.getTotal(userId, toUserId);
        return new PageVo(messages,new Long(total));
    }

    @Override
    public Integer getUnread(Long fromUserId, Long toUserId) {
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Message::getFromUserId,fromUserId);
        wrapper.eq(Message::getToUserId,toUserId);
        wrapper.eq(Message::getIsCheck,SystemConstants.MESSAGE_UNREAD);
        wrapper.last("limit 100");
        return count(wrapper);
    }

    @Override
    public Integer getTotalUnread(Long id) {
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Message::getToUserId,id);
        wrapper.eq(Message::getIsCheck, SystemConstants.MESSAGE_UNREAD);
        wrapper.last("limit 100");
        return count(wrapper);
    }

    @Override
    public void updateMessageIsCheck(String id) {
        LambdaUpdateWrapper<Message> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Message::getMessageId,id);
        wrapper.set(Message::getIsCheck,SystemConstants.MESSAGE_READ);
        update(wrapper);
    }

    @Override
    public ResponseResult addMessage(String message) {
        JSONObject object = JSONObject.parseObject(message);
        Long userID = object.getJSONObject("fromUser").getLong("id");
        Message msg = new Message();
        msg.setCreateBy(userID);
        msg.setCreateTime(object.getDate("sendTime"));
        msg.setMessageId(object.getString("id"));
        msg.setMessage(object.getString("content"));
        msg.setFileName(object.getString("fileName"));
        msg.setFileSize(object.getString("fileSize"));
        msg.setFromUserId(userID);
        msg.setToUserId(object.getLong("toContactId"));
        msg.setType(object.getString("type"));
        save(msg);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getUnreadContractByID(Long id) {
        List<UserContactVo> contracts = baseMapper.getUnreadContractById(id);
        for (UserContactVo contact : contracts) {
            contact.setUnread(getUnread(contact.getId(),id));
        }
        return ResponseResult.okResult(contracts);
    }

    @Override
    public Map<Long,UnreadVo> getUnreadList(Long id) {
        return baseMapper.getUnreadList(id);
    }

}
